package com.yeyks.controller.finance;

import com.google.common.collect.Lists;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.annotation.SystemLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.constants.SysLogMsg;
import com.yeyks.common.em.accountinfo.AccountInfoTypeEnum;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.commission.MiniCommissionRecordParam;
import com.yeyks.common.param.finance.*;
import com.yeyks.common.result.commission.MiniCommissionRecordResult;
import com.yeyks.common.result.withdrawalorder.WithdrawalOrderAgentDownloadResult;
import com.yeyks.common.result.withdrawalorder.WithdrawalOrderDownloadResult;
import com.yeyks.common.utils.excel.ExcelUtil;
import com.yeyks.commonReference.service.hotel.HotelWithdrawalOrderService;
import com.yeyks.commonReference.service.mattress.HotelMattressOverviewService;
import com.yeyks.commonReference.service.vip.MiniVipCommissionBillService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 财务模块接口
 *
 * @author shuai
 * @date 2019-07-09
 */
@Slf4j
@Api(tags = "财务模块接口")
@RestController
@RequestMapping("/finance")
public class FinanceController {

    @Autowired
    private HotelWithdrawalOrderService hotelWithdrawalOrderService;
    @Autowired
    private HotelMattressOverviewService hotelMattressOverviewService;
    @Autowired
    private MiniVipCommissionBillService miniVipCommissionBillService;

    @NeedToken
    @ApiOperation(value = "绑定或修改银行账户", notes = "绑定或修改成功返回true,失败抛出错误信息")
    @PostMapping("/bindOrUpdate")
    public AjaxResponseBody<Boolean> bindOrUpdate(@Validated FinanceBindParam financeBindParam) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (!hotelWithdrawalOrderService.bind(loginUser, financeBindParam)) {
            throw new ServiceException("绑定银行账户失败");
        }
        return AjaxResponseBody.success(true);
    }

    @NeedToken
    @ApiOperation(value = "总公司绑定酒店银行账户", notes = "成功返回true,失败抛出错误信息")
    @PostMapping("/bindOrUpdate/hotel")
    public AjaxResponseBody<Boolean> bindOrUpdateForHotel(@Validated FinanceBindHotelParam financeBindParam) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isTrue(!AccountInfoTypeEnum.Administrator.value().equals(loginUser.getType()), "您无权操作");
        if (!hotelWithdrawalOrderService.bindHotel(financeBindParam)) {
            throw new ServiceException("绑定银行账户失败");
        }
        return AjaxResponseBody.success(true);
    }

    @NeedToken
    @ApiOperation(value = "判断是否可以提现", notes = "可以提现返回true，否则直接提示错误信息")
    @ApiImplicitParam(paramType = "query", name = "type", value = "类型，0营业提现，1床垫分成提现，2vip佣金提现，3小vip佣金提现", required = true, dataType = "int")
    @PostMapping("/check")
    public AjaxResponseBody<Boolean> check(@RequestParam(name = "type") Integer type) {
        Arguments.intIsNull(type, "type必传");
        List<Integer> lists = Lists.newArrayList(0, 1, 2, 3);
        if (!lists.contains(type)) {
            throw new ServiceException("type有误");
        }
        return AjaxResponseBody.success(hotelWithdrawalOrderService.checkWithdrawal(AccountUtils.getLoginUser(), type));
    }

    @SystemLog(value = SysLogMsg.FINANCE_UPDATE)
    @NeedToken
    @ApiOperation(value = "修改提现订单状态", notes = "修改提现订单状态")
    @PostMapping("/update")
    public AjaxResponseBody<Boolean> update(@Validated FinanceOrderUpdateParam param) {
        List<Integer> list = Lists.newArrayList(2, 4, 5, 6);
        if (!list.contains(param.getStatus())) {
            throw new ServiceException("不能修改该状态");
        }
        return AjaxResponseBody.success(hotelWithdrawalOrderService.myUpdate(AccountUtils.getLoginUser(), param));
    }

    @SystemLog(value = SysLogMsg.FINANCE_UPDATE_MORE)
    @NeedToken
    @ApiOperation(value = "批量修改提现订单状态为审核通过", notes = "批量修改提现订单状态为审核通过")
    @PostMapping("/update/more")
    public AjaxResponseBody<Boolean> updateMore(@Validated FinanceOrderUpdateMoreParam param) {
        return AjaxResponseBody.success(hotelWithdrawalOrderService.myUpdateMore(AccountUtils.getLoginUser(), param));
    }

    @NeedToken
    @ApiOperation(value = "获取C2B分佣账户余额", notes = "获取C2B分佣账户余额")
    @PostMapping("/c2b/remain")
    public AjaxResponseBody<Map<String, Object>> c2bRemain() {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        List<Integer> typeLists = Lists.newArrayList(2, 3, 4, 5);
        Arguments.isTrue(!typeLists.contains(loginUser.getType()), "您无权调用");
        return AjaxResponseBody.success(hotelWithdrawalOrderService.c2bRemain(loginUser));
    }

    @NeedToken
    @ApiOperation(value = "获取vip分佣账户余额", notes = "获取vip分佣账户余额")
    @PostMapping("/vip/remain")
    public AjaxResponseBody<Map<String, Object>> vipRemain() {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        List<Integer> typeLists = Lists.newArrayList(10, 11, 12, 5);
        Arguments.isTrue(!typeLists.contains(loginUser.getType()), "您无权调用");
        return AjaxResponseBody.success(hotelWithdrawalOrderService.vipRemain(loginUser));
    }

    @NeedToken
    @ApiOperation(value = "获取小vip分佣账户余额", notes = "获取小vip分佣账户余额")
    @PostMapping("/mini/vip/remain")
    public AjaxResponseBody<Map<String, Object>> miniVipRemain() {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        List<Integer> typeLists = Lists.newArrayList(5, 20, 21, 22);
        Arguments.isTrue(!typeLists.contains(loginUser.getType()), "您无权调用");
        return AjaxResponseBody.success(hotelWithdrawalOrderService.miniVipRemain(loginUser));
    }

    @NeedToken
    @ApiOperation(value = "获取床垫分佣账户余额", notes = "获取床垫分佣账户余额")
    @PostMapping("/mattress/remain")
    public AjaxResponseBody<Map<String, Object>> mattressRemain() {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        List<Integer> typeLists = Lists.newArrayList(2, 3, 4, 5);
        Arguments.isTrue(!typeLists.contains(loginUser.getType()), "您无权调用");
        return AjaxResponseBody.success(hotelMattressOverviewService.remain(loginUser));
    }

    @NeedToken
    @ApiOperation("获取收支账户信息")
    @PostMapping("/info")
    public AjaxResponseBody<FinanceBindParam> getBankInfo() {
        FinanceBindParam financeBindParam = hotelWithdrawalOrderService.getBankInfo(AccountUtils.getLoginUser());
        return AjaxResponseBody.success(financeBindParam);
    }

    /**
     * 提现订单导出
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "提现订单导出", notes = "提现订单导出")
    @PostMapping("/order/download")
    public void orderDownload(@Validated WithdrawalOrderDownloadParam param, HttpServletResponse response) {
        Arguments.isTrue(AccountType.Administrator != AccountUtils.getLoginUser().getType(), "您无权访问");
        if ("5".equals(param.getAccountType())) {
            List<WithdrawalOrderDownloadResult> orderDownloadResults = hotelWithdrawalOrderService.hotelOrderDownload(param);
            ExcelUtil.disposeWriteExcelWithSheets(response, orderDownloadResults, new WithdrawalOrderDownloadResult());
        } else {
            List<WithdrawalOrderAgentDownloadResult> orderDownloadResults = hotelWithdrawalOrderService.agentOrderDownload(param);
            ExcelUtil.disposeWriteExcelWithSheets(response, orderDownloadResults, new WithdrawalOrderAgentDownloadResult());
        }

    }

    @NeedToken
    @ApiOperation(value = "v3.8.1 --> 本酒店佣金记录", response = MiniCommissionRecordResult.class)
    @PostMapping("/mine/commission/list")
    public AjaxResponseBody<PageResult<MiniCommissionRecordResult>> mineCommissionList(MiniCommissionRecordParam param) {
        PageResult<MiniCommissionRecordResult> pageResult = miniVipCommissionBillService.mineCommissionList(AccountUtils.getLoginUser(), param);
        return AjaxResponseBody.success(pageResult);
    }

}
